////////////////////////////////////////////////
// DESCRIPTION:
//    I2C Sample application
//
// Legal Notices:
//   Copyright (c) 2008, Telliam Consulting, LLC.
//   All rights reserved.
//
//   Redistribution and use in source and binary forms, with or without modification,
//   are permitted provided that the following conditions are met:
//
//   * Redistributions of source code must retain the above copyright notice, this list
//     of conditions and the following disclaimer.
//   * Redistributions in binary form must reproduce the above copyright notice, this
//     list of conditions and the following disclaimer in the documentation and/or other
//     materials provided with the distribution.
//   * Neither the name of Telliam Consulting nor the names of its contributors may be
//     used to endorse or promote products derived from this software without specific
//     prior written permission.
//
//   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
//   EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
//   SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
//   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
//   BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
//   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
//   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
//   DAMAGE. 
//
using System;
using System.Collections;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using FusionWare.SPOT.Hardware;
using FusionWare.SPOT;

namespace I2CSample
{
    public class Program
    {
        // shipping oem.config emulator configuration file for this sample application 
        // has 2 SimpleI2CDevice instances.
        //NOTE:
        // As of the RTM SDK the address format is different between the emulator and a real device.
        // On the emulator it is the 7 bit address shifted left by one to force the Read/write bit to 0;
        // on a real device it is just the address (Right justified).  Hopefully, MS will correct the
        // emulator to match the real hardware and prevent the need for conditional compilation here.
        // the EMULATOR define is set in the project configuration settings. Typically you have an
        // Emulator configuration and a non- emulator configuration. 
        // Since there is no real hardware for this device there is only one configuration and this 
        // conditional test serves only as an example (and forewarning! ;-) )
#if EMULATOR
        readonly static ushort[] DeviceAddresses = new ushort[] { 0x7C, 0x7A };
#else
        readonly static ushort[] DeviceAddresses = new ushort[] { 0x3E, 0x3D };
#endif
        public static void Main()
        {
            Debug.Print("I2C Sample...");
            try
            {
                // using statement ensures the Dispose() method is called as soon as
                // the bus is no longer needed. 
                using(I2CBus i2cbus = new I2CBus())
                {
                    SimpleI2CDevice dev1 = new SimpleI2CDevice(i2cbus, DeviceAddresses[0]);
                    SimpleI2CDevice dev2 = new SimpleI2CDevice(i2cbus, DeviceAddresses[1]);
                    Debug.Print("The device at address: " + dev1.Address + " has ID: " + dev1.ChipID.Value);
                    Debug.Print("The device at address: " + dev2.Address + " has ID: " + dev2.ChipID.Value);

                    dev1.Scratch8 = 0x12;
                    dev1.Scratch16 = 0x3456;
                    Debug.Print("Scratch8: " + dev1.Scratch8);
                    Debug.Print("Scratch16: " + dev1.Scratch16);
                }
            }
            catch(Exception e)
            {
                Debug.Print("Exception in I2C Sample");
                Debug.Print(e.ToString());
            }
            Debug.Print("I2C Sample exiting!");
        }
    }
}
